PROJECTENGRAM

ELEMENT TYPE

Element Type

Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Structures #◇Data Types #◇Structured Programming #◇Memory Management


◇Overview

The element type (or base type) of an array determines what kind of data each element can hold.

◇Key Concepts

All elements in a C array must be the same type Element type determines memory per element (sizeof) Common types: int, float, double, char Can also be struct types or pointers

◇Details / Theory

Homogeneous Collection

C arrays are homogeneous — every element has the same data type. This is determined at declaration:

『』
C
int ages[5];       // Element type: int (4 bytes each)
float grades[10];  // Element type: float (4 bytes each)
char name[20];     // Element type: char (1 byte each)
double vals[3];    // Element type: double (8 bytes each)

Effect on Memory

The element type determines the total memory allocated:
int arr[10] → 10 × sizeof(int) = typically 40 bytes char arr[10] → 10 × sizeof(char) = 10 bytes double arr[10] → 10 × sizeof(double) = typically 80 bytes

Arrays of Structs

『』
C
typedef struct {
    char name[50];
    int age;
} Student;

Student class[30];  // Array of 30 Student structs

Arrays of Pointers

『』
C
char *words[5];  // Array of 5 char pointers (e.g., array of strings)

◇Common Pitfalls

Attempting to mix types in a single array — C does not allow this. Forgetting that sizeof(array) returns total bytes, not element count.

◇Summary

TL;DR — Element type defines what data each array element holds. All elements must share the same type. It determines the size of each element and total memory allocation.

Related: ◇Declaration — ◇Size — ◇Memory Allocation — ◇What is an Array Source / Reference:

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Why must an array specify an element type during declaration?

Q2

Can a standard C array hold a mix of int and float variables?

Q3

Which of the following determines the total memory allocated for an array?